package utils

import (
	"net/http"
	"encoding/json"
	"fmt"
)

type H struct {
	Code int 
	Msg string
	Data interface{}
	Rows interface{}
	Total interface{}
}

func RespFail(w http.ResponseWriter,msg string) {
	Resp(w,-1,nil,msg)
}

func RespOK(w http.ResponseWriter,msg string) {
	Resp(w,0,nil,msg)
}

func Resp(w http.ResponseWriter,code int, data interface{}, msg string) {
	w.Header().Set("Content-Type","application/json")
	w.WriteHeader(http.StatusOK)
	h := H{
		Code: code,
		Data: data,
		Msg: msg,
	}
	ret,err := json.Marshal(h)
	if err != nil {
		fmt.Println(err)
	}
	w.Write(ret)
}

func RespOKList(w http.ResponseWriter, data interface{}, total interface{}) {
	RespList(w,0,data,total)
}

func RespList(w http.ResponseWriter,code int, data interface{}, total interface{}) {
	w.Header().Set("Content-Type","application/json")
	w.WriteHeader(http.StatusOK)
	h := H{
		Code: code,
		Rows: data,
		Total: total,
	}
	ret, err := json.Marshal(h)
	if err != nil {
		fmt.Println(err)
	}
	w.Write(ret)
}